package com.xukehong.demo.payment;

import com.xukehong.demo.util.encryption.CertUtil;
import com.xukehong.demo.util.encryption.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import sun.misc.BASE64Encoder;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: xkh
 * @Date: 2019/9/4 11:21
 * @Version 1.0
 */
@Slf4j
public class CertSign {


    //域名
    private String HOST = "https://paydemo.ielpm.com";

    //支付测试地址
    private String payUrl = HOST + "/paygate/v1/h5/cashier";
    //查询测试地址
    private String queryUrl = HOST + "/paygate/v1/kjMixPay";

    //商户编号
    private String mchId = "S20190823042411";
    //密钥
    private String key = "";

    //正式
//    String strPfx = "/root/aggregate_payment_center/src/main/resources/cert/";
    //测试
    String srrPfx = "D:/workCode/demo/src/main/resources/cert/CS20190823042411_20190823160840017.pfx";

    @Test
    public void pay() throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        TreeMap<String, String> treeMap = new TreeMap<>();
        //商户编号
        treeMap.put("merchantNo", mchId);
        //接口版本号 固定
        treeMap.put("version", "v1.1");
        //渠道编号
        treeMap.put("channelNo", "02");
        //交易流水号
        treeMap.put("tranSerialNum", RandomStringUtils.randomNumeric(6));
        //交易时间
        LocalDateTime.now();
        treeMap.put("tranTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        //交易币种
        treeMap.put("currency", "CNY");
        //交易金额 分
        treeMap.put("amount", "100");
        //业务代码
        treeMap.put("bizType", "01");
        //商品名称
        treeMap.put("goodsName", "XX");
        //后台通知地址
        treeMap.put("notifyUrl", "http://www.baidu.com");
        //前台跳转地址
        treeMap.put("returnUrl", "http://www.baidu.com");
        //TODO 买家姓名加密
        String certBuyerName = CertUtil.sign(srrPfx, "1111111", "全渠道");
        treeMap.put("buyerName", certBuyerName);
        //买家id 买家在商品的唯一编号
        treeMap.put("buyerId", "111");
        //用户支付IP
        treeMap.put("ip", "1.1.1.1");
        //省编码
        treeMap.put("province", "5500");
        //风控信息
        TreeMap<String, String> riskInfo = new TreeMap();
        //终端设备的唯一标识
        riskInfo.put("deviceID", "123456");
        //设备类型 1，手机 2，平板，3，手表，4，PC
        riskInfo.put("deviceType", "4");
        //设备所在公网IP
        riskInfo.put("sourceIP", "1.1.1.1");
        //应用提供方账户ID
        riskInfo.put("accountIdHash", "123456");

        String toUrl = SignUtil.addKeyToUrlString(riskInfo);
        toUrl = "{" + toUrl + "}";
        //银行预留手机号
        BASE64Encoder encoder = new BASE64Encoder();
        String risk = encoder.encode(toUrl.getBytes("UTF-8"));
        treeMap.put("riskInfo", risk);
        //签名
        String signToUrl = SignUtil.addKeyToUrlString(treeMap);
        String sign = CertUtil.sign(srrPfx, "1111111", signToUrl);
        treeMap.put("sign", sign);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                params.add(entry.getKey(), entry.getValue());
            }
        }
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        log.info("xxxx 下单请求参数:{}", requestEntity);
        ResponseEntity<String> results = restTemplate.exchange(payUrl, HttpMethod.POST, requestEntity, String.class);
        log.info("xxxx 下单返回参数:{}", results);
    }

}
